﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;

namespace SMSROUTER.Business
{
    public partial class SmsTemplate
    {
        #region Singleton Pattern
        private static object _lock = new object();
        private static List<SmsTemplate> _instance;
        public static List<SmsTemplate> GetInstance()
        {
            lock (_lock)
            {
                if (_instance == null)
                {
                    lock (_lock)
                    {
                        using (var context = new DBDataContext(AppSetting.ConnectionString))
                        {
                            _instance = context.SmsTemplates.ToList();
                        }
                    }
                }
            }
            return _instance;
        }
        public static void ReloadData()
        {
            lock (_lock)
            {
                using (var context = new DBDataContext(AppSetting.ConnectionString))
                {
                    _instance = context.SmsTemplates.ToList();
                }
            }
        }
        #endregion Singleton Pattern

        #region static method
        public static SmsTemplate GetByID(int templateID)
        {
            try
            {
                return GetInstance().Find(p => p.TemplateID == templateID);
            }
            catch
            {
                return null;
            }
        }

        public static SmsTemplate FindItem(Predicate<SmsTemplate> predicate)
        {
            try
            {
                return GetInstance().Find(predicate);
            }
            catch
            {
                return null;
            }
        }

        public static List<SmsTemplate> Paging(DBDataContext context, int currentPage, int pageSize, ref int totalRecord,
            int? cooperateID, int? shortCodeID, string templateParam, int? numOfParam, string formatTemplate)
        {
            var resutl = context.SmsTemplates.AsQueryable();
            if (cooperateID != null)
            {
                resutl = resutl.Where(p => p.CooperateID == cooperateID.Value);
            }
            if (shortCodeID != null)
            {
                resutl = resutl.Where(p => p.ShortCodeID == shortCodeID.Value);
            }
            if (!string.IsNullOrWhiteSpace(templateParam))
            {
                resutl = resutl.Where(p => p.TemplateParameter.Contains(templateParam));
            }
            if (numOfParam != null)
            {
                resutl = resutl.Where(p => p.NumOfParameter == numOfParam.Value);
            }
            if (!string.IsNullOrWhiteSpace(formatTemplate))
            {
                resutl = resutl.Where(p => p.FormatTemplate.Contains(formatTemplate));
            }
            totalRecord = resutl.Count();
            return resutl.Skip(((currentPage - 1) < 0 ? 0 : (currentPage - 1)) * pageSize).Take(pageSize).ToList();
        }
        #endregion static method
    }
}